package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import com.qk.dto.CourseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @GetMapping
    public Result list(CourseDto courseDto){
        log.info("查询课程列表:"+courseDto);
        PageResult<Course> pageResult= courseService.list(courseDto);
        return Result.success(pageResult);

    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除课程:"+id);
        courseService.delete(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("添加课程:"+course);
        courseService.add(course);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id){
        log.info("查询课程:"+id);
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }
    @Log
    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        log.info("修改课程:"+course);
        courseService.updateCourse(course);
        return Result.success();
    }
    @GetMapping("/list")
    public Result allList(){
        log.info("查询所有课程");
        return Result.success(courseService.allList(new Course()));
    }
    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject){
        log.info("查询课程:"+subject);
        List<Course> courses = courseService.ListBySubject(subject);
        return Result.success(courses);
    }
}
